﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///CYBMXX 的摘要说明
/// </summary>
public partial class CYBMXX
{
    public enum typeSex { 男 = 1, 女 };
    public enum typeUser { 个人 = 1, 企业 };
    public static CYBMXX GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.CYBMXX.FirstOrDefault(row => row.ID == id&&row.Enable_Flag==1);
        }
    }

    public static List<CYBMXX> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.CYBMXX.ToList();
        }
    }

    public static List<CYBMXX> GetAllByAddUseID(int AddUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.CYBMXX.Where(row => row.AddUserID == AddUserID&&row.Enable_Flag==1).ToList();
        }
    }

    public static CYBMXX GetIsRankByAddUseID(int AddUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.CYBMXX.FirstOrDefault(row => row.AddUserID == AddUserID && row.Enable_Flag == 1&&row.IsRank==true);
        }
    }



    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.CYBMXX.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<CYBMXX>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.CYBMXX.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.CYBMXX.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static List<CYBMXX> GetAllByConditon(int sheng, int shi, int qu, string name, int typeUser, string telPhone, string phone, string address, int userID,int sexID,string email,string companyName,string contact)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.CYBMXX
                        where t.Enable_Flag == 1
                        select t;
            if (sheng != 0)
                query = query.Where(row => row.ProvinceID == sheng);
            if (shi != 0)
                query = query.Where(row => row.CityID == shi);
            if (qu != 0)
                query = query.Where(row => row.ReginID == qu);
            if (!string.IsNullOrEmpty(name))
                query = query.Where(row => row.UserName.Contains(name));
            if (typeUser != 0)
                query = query.Where(row => row.TypeID == typeUser);
            if (!string.IsNullOrEmpty(telPhone))
                query = query.Where(row => row.Telphone.Contains(telPhone));
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(row => row.Phone.Contains(phone));
            if (!string.IsNullOrEmpty(address))
                query = query.Where(row => row.Address.Contains(address));
            if (userID != 0)
                query = query.Where(row => row.AddUserID == userID);
            if (sexID != 0)
                query = query.Where(row => row.Sex == sexID);
            if (!string.IsNullOrEmpty(email))
                query = query.Where(row => row.Email.Contains(email));
            if (!string.IsNullOrEmpty(companyName))
                query = query.Where(row => row.CompanyName.Contains(companyName));
            if (!string.IsNullOrEmpty(contact))
                query = query.Where(row => row.Contact.Contains(contact));
        
            return query.ToList();
        }
    }

    public static List<CYBMXX> GetAllInUseIDByConditon(int sheng, int shi, int qu, string name, int typeUser, string telPhone, string phone, string address, int userID, int sexID, string email, string companyName, string contact)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.CYBMXX
                        where t.Enable_Flag == 1 && t.AddUserID == userID
                        select t;
            if (sheng != 0)
                query = query.Where(row => row.ProvinceID == sheng);
            if (shi != 0)
                query = query.Where(row => row.CityID == shi);
            if (qu != 0)
                query = query.Where(row => row.ReginID == qu);
            if (!string.IsNullOrEmpty(name))
                query = query.Where(row => row.UserName.Contains(name));
            if (typeUser != 0)
                query = query.Where(row => row.TypeID == typeUser);
            if (!string.IsNullOrEmpty(telPhone))
                query = query.Where(row => row.Telphone.Contains(telPhone));
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(row => row.Phone.Contains(phone));
            if (!string.IsNullOrEmpty(address))
                query = query.Where(row => row.Address.Contains(address));
       
            if (sexID != 0)
                query = query.Where(row => row.Sex == sexID);
            if (!string.IsNullOrEmpty(email))
                query = query.Where(row => row.Email.Contains(email));
            if (!string.IsNullOrEmpty(companyName))
                query = query.Where(row => row.CompanyName.Contains(companyName));
            if (!string.IsNullOrEmpty(contact))
                query = query.Where(row => row.Contact.Contains(contact));

            return query.ToList();
        }
    }

    public static List<CYBMXX> GetAllInUserdByConditon(int sheng, int shi, int qu, string name, int typeUser, string telPhone, string phone, string address, int userID, int sexID, string email, string companyName, string contact)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.CYBMXX
                        where t.Enable_Flag == 1 && t.AddUserID == userID
                        select t;
            if (sheng != 0)
                query = query.Where(row => row.ProvinceID == sheng);
            if (shi != 0)
                query = query.Where(row => row.CityID == shi);
            if (qu != 0)
                query = query.Where(row => row.ReginID == qu);
            if (!string.IsNullOrEmpty(name))
                query = query.Where(row => row.UserName.Contains(name));
            if (typeUser != 0)
                query = query.Where(row => row.TypeID == typeUser);
            if (!string.IsNullOrEmpty(telPhone))
                query = query.Where(row => row.Telphone.Contains(telPhone));
            if (!string.IsNullOrEmpty(phone))
                query = query.Where(row => row.Phone.Contains(phone));
            if (!string.IsNullOrEmpty(address))
                query = query.Where(row => row.Address.Contains(address));
            
            if (sexID != 0)
                query = query.Where(row => row.Sex == sexID);
            if (!string.IsNullOrEmpty(email))
                query = query.Where(row => row.Email.Contains(email));
            if (!string.IsNullOrEmpty(companyName))
                query = query.Where(row => row.CompanyName.Contains(companyName));
            if (!string.IsNullOrEmpty(contact))
                query = query.Where(row => row.Contact.Contains(contact));

            return query.ToList();
        }
    }

    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            CYBMXX objCYBMXX = CYBMXX.GetByID(ID);
            if (objCYBMXX == null)
                return;
            objCYBMXX.Enable_Flag = 0;
            objCYBMXX.Update();
        }
    }
}